package org.jeecg.modules.app.handler;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.rabbitmq.client.Channel;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.boot.starter.rabbitmq.core.BaseRabbiMqHandler;
import org.jeecg.boot.starter.rabbitmq.listenter.MqListener;
import org.jeecg.common.annotation.RabbitComponent;
import org.jeecg.common.base.BaseMap;
import org.jeecg.common.config.TenantContext;
import org.jeecg.common.util.JacksonUtil;
import org.jeecg.modules.app.constant.AppConstant;
import org.jeecg.modules.app.entity.AppPushConfig;
import org.jeecg.modules.app.entity.AppPushRecord;
import org.jeecg.modules.app.service.IAppPushConfigService;
import org.jeecg.modules.app.service.IPushMessageService;
import org.jeecg.modules.app.vo.PushConfigVO;
import org.jeecg.modules.app.vo.PushTargetVO;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Header;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author by sean
 * @desc: 租车支付处理
 * @since 2024-09-24 星期二 15:04:08
 */
@Slf4j
@RequiredArgsConstructor
@RabbitComponent(value = "app.push.messageListener")
public class AppPushMessageHandler extends BaseRabbiMqHandler<BaseMap> {

    private final IAppPushConfigService appPushConfigService;

    private final IPushMessageService pushMessageService;

    @RabbitListener(queues = "app.push.message")
    public void onMessage(BaseMap baseMap, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag) {
        super.onMessage(baseMap, deliveryTag, channel, new MqListener<BaseMap>() {
            @Override
            public void handler(BaseMap map, Channel channel) {
                List<PushTargetVO> targetList = map.get("targetList");
                AppPushRecord message = map.get("message");
                String tenantId = map.get("tenantId");
                TenantContext.setTenant(tenantId);
                try{
                    Map<String, List<PushTargetVO>> listMap = targetList
                            .stream().collect(Collectors.groupingBy(a-> a.getClientPlatform().getValue()));
                    for (Map.Entry<String, List<PushTargetVO>> entry : listMap.entrySet()) {
                        PushConfigVO configVO = appPushConfigService.get(entry.getKey());
                        if(ObjectUtil.isNotEmpty(configVO)){
                            ObjectNode config = JacksonUtil.newJSONObject();
                            config.put("appId", configVO.getAppId());
                            config.put("appKey", configVO.getAppKey());
                            config.put("masterSecret", configVO.getMasterSecret());
                            pushMessageService.broadcast(entry.getValue(), config, message);
                        }
                    }
                }catch(Exception ex){
                    throw new RuntimeException(ex);
                }finally {
                    TenantContext.clear();
                }
            }
        });
    }
}
